package com.aiks.dsyncpluginspringbootautoconfigure.plugin;


import com.aiks.dsyncpluginspringbootautoconfigure.pojo.ModuleConfigEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgPlugParamsEntity;

import java.util.Iterator;
import java.util.List;

/**
 * AbstractConditionPlugin.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @date 2022-06-27 17:29
 * @since 1.0.0
 **/
public abstract class AbstractConditionPlugin<T> extends AbstractPlugin<T> {
    
    /**
     * execute.
     *
     * @param moduleConfig moduleConfig
     * @param saasDatas    saasDatas
     * @param clientDatas  clientDatas
     */
    @Override
    public void execute(final ModuleConfigEntity moduleConfig, final List<T> saasDatas, final List<Object> clientDatas) {
        filter(moduleConfig, saasDatas);
        if (null != getNext()) {
            getNext().execute(moduleConfig, saasDatas, clientDatas);
        }
    }
    
    /**
     * filter.
     *
     * @param moduleConfig moduleConfig
     * @param saasDatas    saasDatas
     */
    public void filter(final ModuleConfigEntity moduleConfig, final List<T> saasDatas) {
        if (isExecute(moduleConfig, getPluginEnum())) {
            List<OrgPlugParamsEntity> orgPlugParamsEntities = moduleConfig.getOrgPlugEntities().stream()
                .filter(orgPlugEntity -> getPluginEnum().getPluginName().equals(orgPlugEntity.getPlugName()))
                .findFirst().get().getOrgPlugParamsEntities();
            Iterator<T> iterator = saasDatas.iterator();
            while (iterator.hasNext()) {
                T t = iterator.next();
                if (doFilter(moduleConfig, t, orgPlugParamsEntities)) {
                    iterator.remove();
                }
            }
        }
    }
    
    /**
     * doFilter.
     *
     * @param moduleConfig          moduleConfig
     * @param t                     t
     * @param orgPlugParamsEntities orgPlugParamsEntities
     * @return Boolean
     */
    protected abstract Boolean doFilter(ModuleConfigEntity moduleConfig, T t,
                                        List<OrgPlugParamsEntity> orgPlugParamsEntities);
    
}
